package zhklappoint.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import zhklappoint.dao.UserActivityMidDao;
import zhklappoint.entity.UserActivityMid;
import zhklappoint.enums.ENUM_ISDELETE;
import zhklappoint.service.UserActivityMidService;
import zhklappoint.util.PageUtils;

@Service
public class UserActiveMidServiceImpl implements UserActivityMidService{

	@Autowired
	UserActivityMidDao uadao;
	
	@Override
	public UserActivityMid saveUserActivityMid(UserActivityMid userActiveMid) {
		
		return uadao.save(userActiveMid);
	}
	
	@Override
	public void deleteUserActivityMid(UserActivityMid userActiveMid) {
		userActiveMid.setIsdelete(ENUM_ISDELETE.DELETED.value);
		uadao.save(userActiveMid);
	}

	@Override
	public Page<UserActivityMid> getUserActtiList(UserActivityMid userActivityMid, Integer page, Integer size) {
		userActivityMid.setIsdelete(ENUM_ISDELETE.NOT_DELETED.value);
        Pageable pageable= PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withMatcher("isdelete", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("comId", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("wxuserId", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("comId", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("userphone", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("orderSign", ExampleMatcher.GenericPropertyMatchers.exact())
                .withIgnorePaths("createdate","updatedate");// 忽略字段，即不管忽略名字是什么值都不加入查询条件
        Example<UserActivityMid> example = Example.of(userActivityMid, matcher);
        return uadao.findAll(example, pageable);
	}

	@Override
	public UserActivityMid getUserActivityMidById(String userActiveId) {
		Optional<UserActivityMid> ou=uadao.findById(userActiveId);
        if(!ou.isPresent()) {
            return null;
        }
        UserActivityMid c=ou.get();
        if(c.getIsdelete()==ENUM_ISDELETE.DELETED.value){
            return null;
        }
        return c;
	}
	//status 0不用是否重复  1用重复频率计算是否重复 2所选服务对象是否在选择时间已经被预约了
	@Override
	public Page<UserActivityMid> getUserTodayActtiList(UserActivityMid userActivityMid, Integer status,Integer timeRate,Integer page, Integer size)  {
		if(ObjectUtils.isEmpty(status)) {
			status=0;
		}
		Pageable pageable = PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("isdelete", ENUM_ISDELETE.NOT_DELETED.value);
		StringBuffer queryString = new StringBuffer("select * from zhkl_user_activity ");
		queryString.append(" WHERE isdelete = :isdelete ");
		if(!ObjectUtils.isEmpty(userActivityMid.getActivId())) {
			values.put("activeId", userActivityMid.getActivId());
			queryString.append(" and activ_id = :activeId");
		}
		if(!ObjectUtils.isEmpty(userActivityMid.getOrderSign())) {
			values.put("ordersign", userActivityMid.getOrderSign());
			queryString.append(" and order_sign = :ordersign");
		}
		if(!ObjectUtils.isEmpty(userActivityMid.getComId())) {
			values.put("comId", userActivityMid.getComId());
			queryString.append(" and com_id = :comId");
		}
		if(!ObjectUtils.isEmpty(userActivityMid.getWxuserId())) {
			values.put("wxUserId", userActivityMid.getWxuserId());
			queryString.append(" and wxuser_id = :wxUserId");
		}
		if(!ObjectUtils.isEmpty(userActivityMid.getChoseObj())) {
			values.put("choseObj", userActivityMid.getChoseObj());
			queryString.append(" and chose_obj = :choseObj");
		}
		if(status==1) {//在重复频率内不可重复预约
			queryString.append(" and end_time is NOT NULL");
			Long nowTim=userActivityMid.getBegTime()-3600*1000*timeRate;
			values.put("endTim", nowTim);
			queryString.append(" and end_time > :endTim");
		}else if(status==2){
			values.put("begTime", userActivityMid.getBegTime());
			values.put("endTime", userActivityMid.getEndTime());
			queryString.append(" and ((end_time <= :endTime and end_time >= :begTime)");
			queryString.append(" or (beg_time <= :endTime and beg_time >= :begTime) ");
			queryString.append(" or (beg_time <= :begTime and end_time >= :endTime)) ");
		}
		
//		if(!ObjectUtils.isEmpty(userActivityMid.getOrderSign())) {
//			values.put("activityName", activtiy.getActivityName());
//			queryString.append(" and activity_name LIKE '%' :activityName '%'");
//		}
		queryString.append(" ORDER BY createdate DESC ");
		StringBuffer countString = new StringBuffer("select count(1) from (").append(queryString).append(") a");
		return uadao.sqlQuery(queryString+"", countString+"", values, pageable,userActivityMid.getClass());
	}
	
	
}
